﻿#region
/* 
 * Descripciòn: Clase que representa una area.
 * Programador: Marco Torres
 * Fecha: 14 Mayo 2012
 * 
 * Log de modificaciones.
 * Descripciòn:
 * Programador:
 * Fecha:
 */
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Services.Contracts.DTO.Infos;
using System.Data;
using Demo.CommonBase.Integration.DAO.Impl;
using System.Data.SqlClient;
using Demo.CommonBase.Logging;
using Demo.Services.Integration.DAO.Contracts.Exceptions;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.Services.Integration.DAO.IntegrationComponents.Mappings;
using Demo.Services.Integration.DAO.IntegrationComponents.Helpers;
using Demo.CommonBase.DTO.Infos;
using Demo.Services.Contracts.DTO.Filters;

namespace Demo.Services.Integration.DAO.Impl
{
    public class AreaDAO : DAOBase
    {
        public AreaResultInfo GetAreas(PaginationInfo pagin, AreaFilter filters)
        {
            AreaResultInfo areaList = null;
            try
            {
                IList<IDbDataParameter> parameters = AreaDAOHelper.GetAreaByPage(pagin, filters);
                DataSet ds = Retrieve("Area_R", parameters);
                if (ValidateDataSet(ds))
                {
                    areaList = AreaDAOMapper.GetAreas(ds);
                }

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "AreaDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "AreaDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return areaList;
        }
        public IList<AreaInfo> GetAreaList()
        {
            IList<AreaInfo> areas = null;
            try
            {
                DataSet ds = Retrieve("Area_R_List");
                if (ValidateDataSet(ds))
                {
                    areas = AreaDAOMapper.GetAreaList(ds);
                }

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "AreaDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "AreaDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return areas;
        }

        public AreaInfo GetAreaById(int id)
        {
            AreaInfo result = null;
            try
            {
                IList<IDbDataParameter> parameters = AreaDAOHelper.GetAreaData(id);
                DataSet ds = Retrieve("Area_RO", parameters);
                if (ValidateDataSet(ds))
                {
                    result = AreaDAOMapper.GetAreaOne(ds);
                }

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "AreaDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "AreaDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public void DeleteArea(int id)
        {
            
            try
            {
                IList<IDbDataParameter> parameters = AreaDAOHelper.GetAreaData(id);
                Create("Area_D", parameters);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "AreaDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "AreaDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        public void UpdateArea(AreaInfo area)
        {
            try
            {
                IList<IDbDataParameter> parameters = AreaDAOHelper.GetAreaUpdate(area);
                Create("Area_U", parameters);

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "AreaDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "AreaDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }

        public void SaveArea(AreaInfo area)
        {
            try
            {
                IList<IDbDataParameter> parameters = AreaDAOHelper.GetAreaSave(area);
                Create("Area_C", parameters);

            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "AreaDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "AreaDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
        }
        public bool CheckName(int id, string name)
        {
            bool isValid = false;
            try
            {
                IList<IDbDataParameter> parameters = AreaDAOHelper.GetCheckNameParams(id, name);
                DataSet ds = Retrieve("Area_R_CheckName", parameters);
                isValid = !ValidateDataSet(ds);
            }
            catch (SqlException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION, "AreaDAO", ex);
            }
            catch (DataException ex)
            {
                Logger.Error(ex);
                throw new ServiceDataException(ServiceDataException.DAO_METHOD_EXCEPTION,
                          "AreaDAO", ex);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return isValid;
        }
    }
}
